package com.ndood.agent.service.user.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ndood.agent.core.constaints.AgentErrCode;
import com.ndood.agent.core.exception.AgentException;
import com.ndood.agent.dao.system.StaffDao;
import com.ndood.agent.dao.system.extend.ExtendStaffDao;
import com.ndood.agent.pojo.system.StaffPo;
import com.ndood.agent.service.user.AccountBindingService;

/**
 * 账号绑定相关业务
 */
@Service
public class AccountBindingServiceImpl implements AccountBindingService{

	@Autowired
	private ExtendStaffDao extendStaffDao;
	
	@Autowired
	private StaffDao staffDao;
	
	@Override
	public boolean isMobileBindingAccounts(String mobile) {
		Long count = extendStaffDao.countByMobile(mobile);
		return count > 0;
	}

	@Override
	public void bindingMobile(String userId, String mobile) throws AgentException {
		
		// Step1: 根据userId查询出用户
		StaffPo staff = staffDao.selectByPrimaryKey(Integer.parseInt(userId));
		if(staff==null) {
			throw new AgentException(AgentErrCode.ERR_OTHER,"用户不存在！");
		}
		
		// Step2: 更新用户信息
		staff.setMobile(mobile);
		staff.setUpdateTime(new Date());
		staffDao.updateByPrimaryKeySelective(staff);
		
	}

	@Override
	public boolean isEmailBindingAccounts(String email) {
		Long count = extendStaffDao.countByEmail(email);
		return count > 0;
	}

	@Override
	public void bindingEmail(String userId, String email) throws AgentException {
		
		// Step1: 根据userId查询出用户
		StaffPo staff = staffDao.selectByPrimaryKey(Integer.parseInt(userId));
		if(staff==null) {
			throw new AgentException(AgentErrCode.ERR_OTHER,"用户不存在！");
		}
		
		// Step2: 更新用户信息
		staff.setEmail(email);
		staff.setEmailStatus(0);
		staff.setUpdateTime(new Date());
		staffDao.updateByPrimaryKeySelective(staff);
		
	}

}
